package com.mls.guardian.executor.master;

import com.mls.guardian.container.MasterContainer;
import com.mls.guardian.protocol.RemoteBody;
import com.mls.guardian.protocol.RemoteCommand;
import com.mls.guardian.protocol.body.AppHeatBeatBody;
import com.mls.guardian.remoting.client.ClientConnect;
import com.mls.guardian.remoting.client.impl.NettyClientConnect;
import com.mls.guardian.remoting.executor.RemoteExecutor;
import com.mls.guardian.remoting.netty.RemoteTools;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by jl on 15/10/23.
 */
public class PingExecutor implements RemoteExecutor{
    private static final Logger log = LoggerFactory.getLogger(PingExecutor.class);
    private MasterContainer container;

    public PingExecutor(MasterContainer container){
        this.container = container;
    }

    @Override
    public RemoteBody execute(ChannelHandlerContext ctx, RemoteCommand request) throws Exception {
        AppHeatBeatBody appHeatBeat = new AppHeatBeatBody();
        appHeatBeat.setAppName("i know");

        String addr = RemoteTools.getRemoteAddr(ctx.channel());
        ClientConnect client = new NettyClientConnect(ctx.channel());
        client.setConnect(ctx.channel());
        container.getClientDispatch().addClient(addr,client);
        log.info("slave client[{}] ping request",ctx.channel().remoteAddress());
        return appHeatBeat;
    }
}
